package org.lee.ico;

import net.sf.image4j.codec.ico.ICODecoder;
import net.sf.image4j.codec.ico.ICOEncoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * IcoConvertDemo
 * ico与图片转换
 *
 * @author dralee
 * @date 2021-12-30 10:51:02
 **/
public class IcoConvertDemo {

    private URL resource(String name) {
        return getClass().getClassLoader().getResource("images/".concat(name));
    }

    private void convertToIco() throws IOException {
        BufferedImage bi = ImageIO.read(resource("icon.png"));
        ICOEncoder.write(bi, new File("png2ico.ico"));
    }

    private void convertFromIco() throws IOException {
        List<BufferedImage> images = ICODecoder.read(new File(resource("favicon.ico").getFile()));
        ImageIO.write(images.get(0), "png", new File("ico2png.png"));
    }

    public static void main(String[] args) throws IOException {
        IcoConvertDemo convert = new IcoConvertDemo();
        convert.convertToIco();
        convert.convertFromIco();
        System.out.println("convert end.");
    }
}
